package com.danger.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.danger.Model.bean.UserInfo;
import com.danger.SysApplication;
import com.danger.Util.ImageLoaderUtil;
import com.danger.Widget.ImageCycleView;
import com.danger.Widget.MyCircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;

public class IndexActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    private long exitTime = 0;
    private ImageButton lost_imageButton;
    private ImageButton found_imageButton;
    private ImageButton category_imageButton;
    private ImageButton mylost_imageButton;
    private ImageButton myfound_imageButton;
    private ImageButton comment_imageButton;
    private ImageCycleView index_imageCycleView;
    private List<ImageCycleView.ImageInfo> list;
    private TextView user_name;
    private ImageView user_photo;
    private NavigationView navigationView;
    private DrawerLayout drawer;
    private View navigationViewHeaderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        SysApplication.getInstance().addActivity(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        initView();
        initEvent();
    }

    private void initView() {
        ImageSize imageSize = new ImageSize(60, 60);
        navigationViewHeaderView = navigationView.getHeaderView(0);
        index_imageCycleView = (ImageCycleView) findViewById(R.id.index_imageCycleView);
        list = new ArrayList<ImageCycleView.ImageInfo>();
        list.add(new ImageCycleView.ImageInfo(R.drawable.first, "广告位1", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.second, "广告位2", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.third, "广告位3", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.first, "广告位4", ""));
        list.add(new ImageCycleView.ImageInfo(R.drawable.second, "广告位5", ""));
        lost_imageButton = (ImageButton) findViewById(R.id.lost_imageButton);
        found_imageButton = (ImageButton) findViewById(R.id.found_imageButton);
        category_imageButton = (ImageButton) findViewById(R.id.category_imageButton);
        mylost_imageButton = (ImageButton) findViewById(R.id.mylost_imageButton);
        myfound_imageButton = (ImageButton) findViewById(R.id.myfound_imageButton);
        comment_imageButton = (ImageButton) findViewById(R.id.comment_imageButton);
        user_name = (TextView) navigationViewHeaderView.findViewById(R.id.user_name);
        user_photo = (MyCircleImageView) navigationViewHeaderView.findViewById(R.id.user_photo);
        UserInfo userInfo = BmobUser.getCurrentUser(this, UserInfo.class);
        user_name.setText(userInfo.getUsername().toString());
        if (userInfo.getPhoto().toString() != null) {
            new ImageLoaderUtil(imageSize, user_photo, userInfo.getPhoto().toString(),this).load();
        }
    }

    private void initEvent() {
        lost_imageButton.setOnClickListener(this);
        found_imageButton.setOnClickListener(this);
        category_imageButton.setOnClickListener(this);
        mylost_imageButton.setOnClickListener(this);
        myfound_imageButton.setOnClickListener(this);
        comment_imageButton.setOnClickListener(this);
        index_imageCycleView.loadData(list, new ImageCycleView.LoadImageCallBack() {
            @Override
            public ImageView loadAndDisplay(ImageCycleView.ImageInfo imageInfo) {
                ImageView imageView = new ImageView(IndexActivity.this);
                imageView.setImageResource(Integer.parseInt(imageInfo.image.toString()));
                return imageView;
            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.index, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.personal_info) {
            startActivity(new Intent(IndexActivity.this, PersonalInfoActivity.class));
        } else if (id == R.id.manage) {

        } else if (id == R.id.feedback) {
            startActivity(new Intent(IndexActivity.this, FeedbackActivity.class));
        } else if (id == R.id.about_us) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lost_imageButton:
                startActivity(new Intent(IndexActivity.this, LostActivity.class));
                break;
            case R.id.found_imageButton:
                startActivity(new Intent(IndexActivity.this, FoundActivity.class));
                break;
            case R.id.category_imageButton:
                break;
            case R.id.mylost_imageButton:
                startActivity(new Intent(IndexActivity.this, MyLostActivity.class));
                break;
            case R.id.myfound_imageButton:
                startActivity(new Intent(IndexActivity.this, MyFoundActivity.class));
                break;
            case R.id.comment_imageButton:
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                SysApplication.getInstance().exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
